import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({
  // disable right-up round
  showSpinner:false
})

// 如何创建Router实例
// 如何切换路由模式
// createWebHistory()
// import.meta.env.BASE_URL 的含义是：在编译时，webpack 会将这个值替换为部署应用时的 URL 的基地址。
// 默认情况下为 / ，可以在vite.config.ts/js中进行修改
// 在普通的模式下html 只有import.meta ,在vue情况下才有env
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login', component: () => import('@/views/Login/index.vue'),
      meta:{
        title:'登录'
      }
    },
    {
      path:'/user/patient',
      component:()=>import('@/views/User/PatientPage.vue'),
      meta:{
        title:'家庭档案'
      }
    },
    {
      path:'/consult/fast',
      component:()=>import('@/views/Consult/ConsultFast.vue'),
      meta:{
        title:'极速问诊'
      }
    },
    {
      path:'/consult/dep',
      component:()=>import('@/views/Consult/ConsultDep.vue'),
      meta:{
        title:'选择科室'
      }
    },
    {
      path:'/consult/illness',
      component:()=>import('@/views/Consult/ConsultIllness.vue'),
      meta:{
        title:'病情描述'
      }
    },
    {
      path:'/consult/pay',
      component:()=>import('@/views/Consult/ConsultPay.vue'),
      meta:{
        title:'问诊支付'
      }
    },

    {
      path:'/address',
      component:()=>import('@/views/Address/UserAddress.vue'),
      meta:{
        title:'地址管理'
      }
    },
    {
      path:'/order/pay/result',
      component:()=>import('@/views/Order/OrderPayResult.vue'),
      meta:{
        title:'药品支付结果'
      }
    },
    {
      path:'/order/:id',
      component:()=>import('@/views/Order/OrderDetail.vue'),
      meta:{
        title:'药品订单详情'
      }
    },
    {
      path:'/order/logistics/:id',
      component:()=>import('@/views/Order/OrderLogistics.vue'),
      meta:{
        title:'物流详情'
      }
    },
    {
      path:'/address/addaddress',
      component:()=>import('@/views/Address/components/AddAddress.vue'),
      meta:{
        title:'新增收货人信息'
      }
    },
    {
      path:'/room',
      component:()=>import('@/views/Room/index.vue'),
      meta:{
        title:'问诊室'
      },
      beforeEnter(to){
        if(to.query.payResult === 'false'){
          return '/user/consult'
        }
      }
    },
    {
      path:'/user/consult',
      component:()=>import('@/views/User/ConsultPage.vue'),
      meta:{
        title:'问诊记录'
      }
    },
    {
      path:'/user/consult/:id',
      component:()=>import('@/views/User/ConsultDetail.vue'),
      meta:{
        title:'问诊详情'
      }
    },
    {
      path:'/order/pay',
      component:()=>import('@/views/Order/OrderPay.vue'),
      meta:{
        title:'药品支付'
      }
    },
    {
      path:'/',
      redirect:'/home',
      component:()=>import('@/views/Layout/index.vue'),
      children:[
        {path:'/home',component:()=>import('@/views/Home/index.vue' ),
          meta:{title:'首页'}
        },
        {path:'/article',component:()=>import('@/views/Article/index.vue'),
          meta:{title:'健康百科'}
        },
        {path:'/notify',component:()=>import('@/views/Notify/index.vue'),
          meta:{title:'消息通知'}
        },
        {path:'/user',component:()=>import('@/views/User/index.vue'),
          meta:{title:'个人中心'}
        },
      ]
    },

  ],
})

// 全局的导航守卫
router.beforeEach((to)=>{
  NProgress.start()
  // take token
  const stroe = useUserStore()
  // white name list
  const whiteList = ['/login']
  // if not have token and not white name list ,to login Page
  if(!stroe.user?.token && !whiteList.includes(to.path)) return '/login'

})

// 全局的后置导航
router.afterEach((to)=>{
  document.title = `${to.meta.title || ''}-优医问诊`
  NProgress.done()
})

export default router
